<template>
    <div class="seach">
      <div class="seach_seach">

        <div class="seach_input1">
        <input type="email" class="form-control" id="exampleFormControlInput1_1" v-model="bookName" placeholder="请输入书名">
       </div>
        <!-- <el-button class="input_buttons">默认按钮</el-button> -->
        <el-button size="small" class="input_buttons" type="primary" @click="this.book_seach">搜索</el-button>
      </div>
      <div class="seach_body">
        <!-- 开始 -->
        <el-table :data="this.users_Info" border stripe><!-- 带边框、斑马纹 -->
          <!-- <el-table-column type="index"></el-table-column> -->
          <el-table-column label="图书编号" prop="bookId"></el-table-column>
          <el-table-column label="书名" prop="bookName"></el-table-column>
          <el-table-column label="作者" prop="author"></el-table-column>
          <el-table-column label="出版社" prop="publish"></el-table-column>
          <el-table-column label="图书类别" prop="category"></el-table-column>
          <el-table-column label="图书价格" prop="price"></el-table-column>
          <el-table-column label="归还时间" prop="deadline"  ></el-table-column>
          <!-- <el-table-column label="身份" prop="ismaind"></el-table-column>  -->

          <el-table-column label="操作" width="200px">
              <template slot-scope="scope">
                <el-tooltip effect="dark" content="修改" placement="top">
                    <el-button type="primary" icon="el-icon-edit" size="mini"  @click="updata_data(scope.row)"></el-button>
                  </el-tooltip>
                  <el-tooltip effect="dark" content="删除" placement="top">
                    <el-button type="danger" icon="el-icon-delete" @click="updata_delete(scope.row)" size="mini"></el-button>
                  </el-tooltip>
              </template>
          </el-table-column>
        <!-- </div> -->
        </el-table>
        <!-- 结束 -->
        <Notice v-if="this.notice" :tonote="this.tonote" :updata=" this.getUsersList"></Notice>
        <Delete v-if="this.delete" :todelete="this.todelete" ></Delete>
      </div>
      <div class="seach_bottom">
        <div class="bottom_buttons">
          <el-pagination
           background
           layout="prev, pager, next"
            @current-change="handleSizeChange"   
           @prev-click="handleCurrentChange"
           @next-click="handleCurrentChange"
           :total=this.total>
          </el-pagination>
        </div>
        <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum" :page-sizes="[1, 2, 5, 10]" :page-size="queryInfo.pagesize" 
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination> -->
      </div>
    </div>
  </template>
  <script>
  import Notice from '../components/book_notice.vue'
  import Delete from '../components/Delete.vue'
import { log } from 'three';
  export default({
    data(){
        return{
          key:0,
            bookName:"", 
            deleteid:"",
            total:10,
            notice:false,
            delete:false, 
            users_Info:[
            ],
            tonote:{},
            test:[{bookId:"20100140205",bookName:"123455",author:"kuangfeng",publish:"男",category:"123456@qq.com",price:"12345678",deadline:0},
          ]
        }
    },
    components:{
      Notice,
      Delete,
    },
    methods:{
      async getUsersList() {
        this.axios.get('http://192.168.43.171:8081/books/page', {
          params:{
                        key:this.key,
                      }
                  }).then( (response)=> {
                    console.log(response.data);
                    this.users_Info=response.data.data;
                    console.log("1234");
                    console.log(this.users_Info);
                    this.total = response.data.maxpage*10
                  }).catch(function (error) {
                    console.log(error);
                  });
      },
      // 当前页
      handleSizeChange(val) {
        this.key=val
        this.axios.get('http://192.168.43.171:8081/books/page', {
                      params:{
                        key:val,
                        bookName:this.bookName,
                      }
                   // 参数 name
                }).then( (response)=> {
                  this.total=response.data.maxpage*10
                  console.log(response);
                  this.users_Info=response.data.data;
                }).catch(function (error) {
                  console.log(error);
                });
        console.log(`当前 ${val} 页`);
      },
      // 下一页或者上一页
      handleCurrentChange(val) {
        console.log(`去第 ${val}页`);
        this.key=val
        this.axios.get('http://192.168.43.171:8081/books/page', {
                      params:{
                        key:val,
                        bookName:this.bookName,
                      }
                   // 参数 name
                }).then( (response)=> {
                  this.total=response.data.maxpage*10
                  console.log(response);
                  this.users_Info=response.data.data;
                }).catch(function (error) {
                  console.log(error);
                });

      },
          // 给图书修改
      updata_data(row){
        this.notice=true;
        this.delete=false;
        this.tonote=row;
        console.log(this.tonote);
        // 
      },
      // 删除图书
      updata_delete(row){
        this.notice=false;
        this.delete=true;
        this.deleteid=row.bookId
      },
      // 删除图书后被调用的delete调用函数
      todelete(){
        console.log("图书信息删除被调用了");
        this.axios.get('http://192.168.43.171:8081/books/delete', {
                      params:{
  
                        bookId:this.deleteid
                      }
                   // 参数 name
                }).then( (response)=> {
                  console.log(response);
                  this.getUsersList();
                  this.$message({
                 message: response.data.msg,
                 type: 'success'
                });
                }).catch(function (error) {
                  console.log(error);
                });
      },
      // seach book
      book_seach(){
      this.axios.get('http://192.168.43.171:8081/books/page', {
                      params:{
                        bookName:this.bookName,
                      }
                   // 参数 name
                }).then( (response)=> {
                  this.total=response.data.maxpage*10
                  console.log(response.data.maxpage);
                  this.users_Info=response.data.data;
                }).catch(function (error) {
                  console.log(error);
                });
    }
    },
    mounted(){
          // 绑定事
          this.$bus.$on ('bus_delete_cancel', (data) => {
            this.delete=data;
            // console.log(data);
            if(data==1){
              this.delete=false
            }
          }),
          this.$bus.$on ('bus_delete', (data) => {
            // this.delete=data;
            console.log(data);
            if(data==1){
              this.delete=false
            }
          })
          this.$bus.$on ('bus_up_date_cancel', (data) => {
            // this.delete=data;
            if(data==1){
              this.notice=false
            }
          })
          this.$bus.$on ('bus_up_date', (data) => {
            // this.delete=data;
            if(data==1){
              this.notice=false
            }
          })
          this.getUsersList();
      },
      beforeDestroy(){
          // this.$bus.$off('bus_delete_cancel')
          this.$bus.$off('bus_delete')
          this.$bus.$off('bus_updata_cancel')
          this.$bus.$off('bus_updata')
          
        },
    beforeRouteEnter:(to,from,next)=>{
   const ismaind=sessionStorage.getItem("ismaind")
    if (ismaind==1) {
      next()
    } else {
      alert("无权限")
    }
  }
  })
  </script>
  <style lang="less" scoped>
  .seach{
    width: 100%;
    height: 100%;
    position: absolute;
    .seach_seach{
      position: absolute;
      right: 50px;
      height: 6%;
      width: 70%;
      // background-color: #67c58b;
      .seach_input{
         position: absolute;
         width: 40%;
         top: 4px;
         left: 50;
      }
      .seach_input1{
       position: absolute;
       width: 40%;
       top: 4px;
       left: 358px;
    }
      .input_buttons{
       position: absolute;
       top: 16%;
       right: 11%;
      }
    }
    .seach_body{
    // background-color: aquamarine;
    // background-color: rgb(245, 243, 243);
    background-color: #fff;
    position: absolute;
    top: 7%;
    height: 80%;
    width: 100%;
    td{
      background-color: #fff;
    }
    }
  
    .seach_bottom{
      // background-color: #5f9ca1;
      height: 10%;
      width: 100%;
      bottom: 1%;
      position: absolute;
      .bottom_buttons{
        position: absolute;
        top: 32%;
        height: 40%;
        width: 20%;
        left: 25%;
        // transform: translate(-50%,-50%);
        // background-color: bisque;
      }
  
    }
  }
  </style>